﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AD.BLL;
using AD.Model;
using AD.DAL;
using System.Xml;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

        DateTime date = DateTime.Parse("2008-5-12").AddMonths(46);
        Console.WriteLine(date.Year);
        string location = UtilHepler.GetLocation(Request.UserHostAddress.Trim());
        Response.Write("result:" + location);
        //string xml =Server.MapPath("");
        ////string xml = HttpModule.RequestSelfToGETString(string.Format(UtilHepler.POST_IP, "113.90.37.4 ", DateTime.Now.ToString("hhmmss")));
        //Console.WriteLine(xml);

        
        //bool d = bool.Parse("true");
        //Console.WriteLine(d);
        //string str = "var user ={\"username\":\"{0}\",\"balancePoint\":{0}}";
        //str = string.Format(str, 33, 55);
        //Console.WriteLine(str);
        //LogBLL.Insert(new LogMod() { IpAddress = "sds", LoginDate = DateTime.Now, UserName = "12313@" });
        //SetBindAccount();
        //string result = "{ \"retcode\":1,\"items\":[{\"a\":1},{\"a\":2}]}";
        ////result = "{\"username\": \"andy\"," +
        //// "\"age\": 20," +
        //// "\"info\": { \"tel\": \"123456\", \"cellphone\": \"98765\" }," +
        //// "\"address\":[{ \"city\": \"beijing\", \"postcode\": \"222333\" },{ \"city\": \"newyork\", \"postcode\": \"555666\" }]}";
        ////Response.Write(result);
        //result = PayTradeToJson("", 10, 1);
        //Response.Write(result);
        //Response.End();
    }

    string PayTradeToJson(string filter, int pageSize, int index)
    {
        int maxcount;
        List<PayTradeMod> pays = PayTradeBLL.QueryByPage(filter, pageSize, index == 0 ? 0 : index - 1, out maxcount);
        int MaxPage = maxcount / pageSize;
        if (maxcount % pageSize != 0)
            MaxPage += 1;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        if (pays == null)
        {
            sb.Append("{\"retcode\":-1}");
            return sb.ToString();
        }
        else
            sb.Append("{\"retcode\":1");
        sb.Append(",");
        sb.Append(SetValue("pageIndex", index));
        sb.Append(",");
        sb.Append(SetValue("maxcount", maxcount));
        sb.Append(",");
        sb.Append(SetValue("maxpage", MaxPage));
        sb.Append(",");
        sb.Append("\"items\":[");
        foreach (var pay in pays)
        {
            sb.Append("{");
            sb.Append(SetValue("Out_Trade_No", pay.Out_Trade_No));
            sb.Append(",");
            sb.Append(SetValue("RewardPoints", pay.RewardPoints));
            sb.Append(",");
            sb.Append(SetValue("RewardType", (int)pay.RewardType));
            sb.Append(",");
            sb.Append(SetValue("Total_fee", pay.Total_fee));
            sb.Append(",");
            sb.Append(SetValue("UserId", pay.UserId));
            sb.Append(",");
            sb.Append(SetValue("UserName", pay.UserName));
            sb.Append(",");
            sb.Append(SetValue("Body", pay.Body));
            sb.Append(",");
            sb.Append(SetValue("Commit_Date", pay.Commit_Date));
            sb.Append(",");
            sb.Append(SetValue("GiftPoints", pay.GiftPoints));
            sb.Append(",");
            sb.Append(SetValue("IsInMoney", pay.IsInMoney));
            sb.Append(",");
            sb.Append(SetValue("Rechargepoints", pay.Rechargepoints));
            sb.Append(",");
            sb.Append(SetValue("Status", pay.Status));
            sb.Append(",");
            sb.Append(SetValue("Subject", pay.Subject));
            sb.Append("}");
            //sb.Append(",");
        }
        sb.Append("]}");
        return sb.ToString();
    }

    void SetBindAccount() {
        List<PayTradeMod> pays = PayTradeBLL.QueryFilterToList("");
       System.Text.StringBuilder sb = new System.Text.StringBuilder();
       sb.Append("{\"retcode\":1,");
       sb.Append("\"items\":[");
        foreach (var pay in pays) {
            sb.Append("{");
            sb.Append(SetValue("Out_Trade_No", pay.Out_Trade_No));
            sb.Append(",");
            sb.Append(SetValue("RewardPoints", pay.RewardPoints));
            sb.Append(",");
            sb.Append(SetValue("RewardType",(int) pay.RewardType));
            sb.Append(",");
            sb.Append(SetValue("Total_fee", pay.Total_fee));
            sb.Append(",");
            sb.Append(SetValue("UserId", pay.UserId));
            sb.Append(",");
            sb.Append(SetValue("UserName", pay.UserName));
            sb.Append(",");
            sb.Append(SetValue("Body", pay.Body));
            sb.Append(",");
            sb.Append(SetValue("Commit_Date", pay.Commit_Date));
            sb.Append(",");
            sb.Append(SetValue("GiftPoints", pay.GiftPoints));
            sb.Append(",");
            sb.Append(SetValue("IsInMoney", pay.IsInMoney));
            sb.Append(",");
            sb.Append(SetValue("Rechargepoints", pay.Rechargepoints));
            sb.Append(",");
            sb.Append(SetValue("Status", pay.Status));
            sb.Append(",");
            sb.Append(SetValue("Subject", pay.Subject));
            sb.Append("}");
        }
        sb.Append("]}");
        Response.Write(sb.ToString());
        Response.End();
    }

    string SetValue(string propertyName, string value)
    {
        return "\"" + propertyName + "\":\"" + value + "\"";
    }

    string SetValue(string propertyName, DateTime value)
    {
        return SetValue(propertyName, value.ToString("yyyy-MM-dd HH:mm:ss"));
    }

    string SetValue(string propertyName, int value)
    {
        return "\"" + propertyName + "\":" + value;
    }
    string SetValue(string propertyName, double value)
    {
        return "\"" + propertyName + "\":" + value;
    }

    string SetValue(string propertyName, bool value)
    {
        return "\"" + propertyName + "\":" + value.ToString().ToLower();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int Ncount = 3;
        for (int N = 0; N <= Ncount; N++)
        {
            SetProcessBar(string.Format("当前进度:{0}/{1}", N, Ncount), N == 0 ? true : false);
            int count = 10;
            for (int i = 0; i < count; i++)
            {
                ProcessBar(i, count);
                System.Threading.Thread.Sleep(1000);
            }

        }
    }
    protected void ProcessBar(int cuttentInt, int totalInt)
    {
        Response.Write("<script>setPgb(" + ((cuttentInt + 1) * 100) / totalInt + ");</script>");
        Response.Flush();
    }
    protected void SetProcessBar(string msg, bool IsInit)
    {
        if (IsInit)
        {
            string strFileName = Request.PhysicalApplicationPath + "\\progressbar.htm";//html路径
            System.IO.StreamReader sr = new System.IO.StreamReader(strFileName, System.Text.Encoding.Default);
            string strHtml = sr.ReadToEnd();
            Response.Write(strHtml);
            sr.Close();
            Response.Flush();
        }
        Response.Write("<script>initPgb('" + msg + "');</script>");
        Response.Flush();
    }
}